package ouhenry.stage.manage.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.collect.Lists;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import ouhenry.stage.common.base.Result;

@RestController
@RequestMapping(value = "/test")
@Api(tags = "这是测试接口")
public class DemoController {
    Logger logger = LoggerFactory.getLogger(DemoController.class);
    

    // ajax请求，没有@Responsebody注解的时候,后台成功接收到数据，但是前端报 404 错误。
    // 原因：由于Controller忘记写返回状态码 @Responsebody注解 ，Spring对返回值映射时找不到对应的视图，所以返回404
    @ApiOperation("这是一个接口")
    @GetMapping(value = "/hello")
    public Result<Long> hello(@RequestParam("name") String name) {
        logger.info("这是测试接口" + name);
        return Result.success(9007199254740991L);
    }

    @ApiOperation("这是一个字典测试接口")
    @ApiImplicitParam(name = "code", value = "字典", required = true, dataType = "String")
    @GetMapping(value = "/dictionary")
    public Result<List<Dictionary>> dictionary(@RequestParam("code") String code) {
        logger.info("这是一个字典测试接口");
        List<Dictionary> lists = Lists.newArrayList();
        if (code.equals("book_type")) {
            lists.add(new Dictionary(1, "收入"));
            lists.add(new Dictionary(2, "支出"));
            lists.add(new Dictionary(3, "转入"));
            lists.add(new Dictionary(4, "转出"));
        }
        // 收入1支出2，转入3，转出4……
        return Result.success(lists);
    }

    class Dictionary {
        private Integer key;
        private String value;

        public Dictionary(Integer key, String value) {
            super();
            this.key = key;
            this.value = value;
        }

        public Integer getKey() {
            return key;
        }

        public void setKey(Integer key) {
            this.key = key;
        }

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }

    }
}
